package com.renwei.yunlong.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.renwei.yunlong.R;
import com.renwei.yunlong.base.BaseActivity;
import com.renwei.yunlong.bean.ServerInfo;
import com.renwei.yunlong.utils.CommonUtils;
import com.renwei.yunlong.utils.StringUtils;
import com.zyf.fwms.commonlibrary.utils.FormatUtil;
import com.zyf.fwms.commonlibrary.utils.StatusBarUtil;

import org.litepal.crud.DataSupport;

/**
 * Project :  yunna.
 * Package name: com.renwei.yunlong
 * Created by :  Benjamin.
 * Created time: 2018/3/15 9:56
 * Changed by :  Benjamin.
 * Changed time: 2018/3/15 9:56
 * Class description:
 */

public class ServerEditActivity extends BaseActivity implements View.OnClickListener {
    private TextView ivBack;
    private TextView tvTitle;
    private TextView tvRight;
    private EditText etServerName;
    private EditText etServerAddress;
    private EditText etServerPort;
    private ServerInfo targeInfo;
    private RadioGroup rgServerType;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        StatusBarUtil.setColor(this, CommonUtils.getColor(this, R.color.title_color), 0);
        setContentView(R.layout.activity_server_edit);
        initView();
    }


    private void initView() {
        ivBack = findViewById(R.id.iv_back);
        ivBack.setOnClickListener(this);
        tvTitle = findViewById(R.id.tv_title);
        tvRight = findViewById(R.id.tv_right);
        tvRight.setOnClickListener(this);
        etServerName = findViewById(R.id.et_server_name);
        etServerAddress = findViewById(R.id.et_server_address);
        etServerPort = findViewById(R.id.et_server_port);
        rgServerType = findViewById(R.id.rg_server_type);
        String servcerName = StringUtils.value(getIntent().getStringExtra("serverName"));
        tvTitle.setText("编辑网络设置");
        tvRight.setText("保存");
        targeInfo = DataSupport.where("serverName=?", servcerName).findFirst(ServerInfo.class);
        if (targeInfo != null) {
            etServerName.setText(StringUtils.value(targeInfo.getServerName()));
            etServerName.setSelection(StringUtils.value(targeInfo.getServerName()).length());
            etServerAddress.setText(StringUtils.value(targeInfo.getAddress()));
            etServerAddress.setSelection(StringUtils.value(targeInfo.getAddress()).length());
            etServerPort.setText(StringUtils.value(targeInfo.getPort()));
            etServerPort.setSelection(StringUtils.value(targeInfo.getPort()).length());
            if (String.valueOf(targeInfo.getType()).contains("https://")) {
                rgServerType.check(R.id.rb_https);
            } else if (String.valueOf(targeInfo.getType()).contains("http://")) {
                rgServerType.check(R.id.rb_http);
            } else {
                targeInfo.setType("https://");
                targeInfo.updateAll("serverName=?", StringUtils.value(targeInfo.getServerName()));
            }
        }

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            default:
                break;
            case R.id.iv_back:
                this.finish();
                break;
            case R.id.tv_right:
                String serverType = "";
                switch (rgServerType.getCheckedRadioButtonId()) {
                    case R.id.rb_https:
                        serverType = "https://";
                        break;
                    case R.id.rb_http:
                        serverType = "http://";
                        break;
                    default:
                        break;
                }
                String name = StringUtils.value(etServerName.getText().toString());
                String ip = StringUtils.value(etServerAddress.getText().toString());
                String port = StringUtils.value(etServerPort.getText().toString());
                if (StringUtils.value(name).trim().length() == 0) {
                    showCenterInfoMsg("服务器名称不能为空");
                    return;
                }
                if (!FormatUtil.isIpAddress(ip)) {
                    showCenterInfoMsg("IP地址格式不正确");
                    return;
                }
                saveServerInfo(serverType, name, ip, port);
                break;
        }
    }


    public void saveServerInfo(String type, String name, String ip, String port) {
        ServerInfo info = new ServerInfo();
        info.setServerName(name);
        info.setType(type);
        info.setAddress(ip);
        info.setPort(port);
        info.setEditable("0");
        String thisName = targeInfo == null || targeInfo.getServerName() == null ? "" : StringUtils.value(targeInfo.getServerName());
        boolean result = info.saveOrUpdate("serverName=?", thisName);
        if (result) {
            showCenterSuccessMsg(tvRight.getText().toString() + "成功");
            this.finish();
        } else {
            showCenterSuccessMsg(tvRight.getText().toString() + "失败");
        }

    }

    public static void openActivity(Context context, String serverName) {
        context.startActivity(new Intent(context, ServerEditActivity.class).putExtra("serverName", serverName));
    }


}
